<?php
namespace App\HttpController\Api;

use App\Model\UserModel;
use EasySwoole\Http\Message\Status;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

class _User extends ApiBase
{
    protected $noNeedLogin = ['login', 'register'];
    // function onRequest(?string $action): ?bool
    // {
    //     return parent::onRequest($action, $this->noNeedLogin);
    // }

    /**
     * 获取用户信息
     */
    function info()
    {
        return $this->success("success", $this->auth->getUserinfo());
    }

    public function login()
    {
        $account  = $this->input('account');
        $password = $this->input('password');

        if (!$account || !$password) return $this->error('请求参数不完整');

        //登录太频繁限制
        // $opt_time = cache("loginTime:{$account}");
        // if($opt_time>10) return $this->error('登录太频繁,请稍后操作');
        // cache("loginTime:{$account}",$opt_time+1, 60);

        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            return $this->success("success", $data);
        } else {
            return $this->error($this->auth->getError());
        }
    }


}
